<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px" size="mini"
             @submit.native.prevent>
      <el-form-item label="模版名称" prop="templateName">
        <el-input v-model="queryParams.templateName" clearable placeholder="请输入" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermi="['cld:checkTemplate:add']" icon="el-icon-plus" plain size="mini" type="primary"
                   @click="handleAdd">新建考核模版
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="checkTemplateList" border>
      <el-table-column align="center" label="模版名称" prop="templateName" width="120px;"/>
      <el-table-column align="center" label="角色" prop="checkRoleName" width="80px;"/>
      <el-table-column align="center" label="考核指标">
        <template slot-scope="scope">
          <el-table :data="scope.row.indicatorList" border>
            <el-table-column align="center" label="指标名称" prop="indicatorName" width="150px;"/>
            <el-table-column align="center" label="权重(%)" prop="weight" width="80px;"/>
            <el-table-column align="center" label="定性/定量指标" prop="indicatorType" width="120px;">
              <template slot-scope="scope">
                <span>{{ scope.row.indicatorType == 1 ? "定性" : "定量" }}</span>
              </template>
            </el-table-column>
            <el-table-column align="center" label="计量单位" prop="indicatorUnit" width="80px;"/>
            <el-table-column align="center" label="指标定义" prop="indicatorDefinition" show-overflow-tooltip
                             width="100px;"/>
            <el-table-column align="center" label="评分标准" prop="indicatorStandard" show-overflow-tooltip
                             />
            <el-table-column align="center" label="评分依据" prop="indicatorBasis" show-overflow-tooltip
                             width="200px;"/>
          </el-table>
        </template>
      </el-table-column>
      <el-table-column align="center" label="总权重%" prop="sumWeight" width="100px;"/>
      <el-table-column align="center" label="提交人" prop="createName" width="100px;"/>
      <el-table-column align="center" label="提交时间" prop="createTime" width="100px;">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="更新人" prop="updateName" width="100px;"/>
      <el-table-column align="center" label="更新时间" prop="updateTime" width="100px;">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作" width="120px;">
        <template slot-scope="scope">
          <el-button v-hasPermi="['cld:checkTemplate:edit']" icon="el-icon-edit" size="mini" type="text"
                     @click="handleUpdate(scope.row)">修改
          </el-button>
          <el-button v-hasPermi="['cld:checkTemplate:remove']" icon="el-icon-delete" size="mini" type="text"
                     @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total"
                @pagination="getList"/>

    <!-- 添加或修改考核模版对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="60%">
      <el-form ref="form" :model="form" :rules="rules" label-position="top" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="模版名称" prop="templateName">
              <el-input v-model="form.templateName" placeholder="请输入"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="考核角色" prop="checkRole">
              <el-select v-model="form.checkRole" placeholder="请选择">
                <el-option v-for="item in categoryOptions" :key="item.jobCategoryId" :label="item.jobName"
                           :value="item.jobCategoryId"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="left">考核指标
          <el-button icon="el-icon-plus" plain size="mini" style="margin-left: 20px" type="primary"
                     @click="handleAddIndicator">选择指标
          </el-button>
        </el-divider>
        <el-table :data="form.indicatorList">
          <el-table-column align="center" label="指标名称" prop="indicatorName" width="150px;"/>
          <el-table-column align="center" label="权重(%)" prop="weight" width="80px;">
            <template slot-scope="scope">
              <el-input v-model="scope.row.weight" type="number" @input="calculatWeighte"></el-input>
            </template>
          </el-table-column>
          <el-table-column align="center" label="定性/定量指标" prop="indicatorType" width="120px;">
            <template slot-scope="scope">
              <span>{{ scope.row.indicatorType == 1 ? "定性" : "定量" }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="计量单位" prop="indicatorUnit" width="80px;"/>
          <el-table-column align="center" label="指标定义" prop="indicatorDefinition" width="100px;"/>
          <el-table-column align="center" label="评分标准" prop="indicatorStandard"/>
          <el-table-column align="center" label="评分依据" prop="indicatorBasis" width="200px;"/>
          <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
            <template slot-scope="scope">
              <el-button icon="el-icon-delete" size="mini" type="text" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-row>
          <el-col :span="12">
            <el-form-item label="指标总权重(%)" prop="sumWeight">
              <el-input v-model="form.sumWeight" :readonly="true" type="number"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 选择指标 -->
    <el-dialog :close-on-click-modal="false" :destroy-on-close="true" :visible.sync="indicatorOpen" append-to-body title="选择指标"
               width="50%">
      <selectedIndicator v-if="indicatorOpen" :notIndicatorIds="notIndicatorIds"
                         @SelectionChange="handleIndicatorSelectionChange">
      </selectedIndicator>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitSelectedIndicator">确 定</el-button>
        <el-button @click="indicatorOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addCheckTemplate,
  delCheckTemplate,
  getCheckTemplate,
  listCheckTemplate,
  updateCheckTemplate,
} from "@/api/cld/checkTemplate";

import selectedIndicator from '../checkIndicator/selectedIndicator.vue';
import {getListCategory} from '@/api/cld/category'

export default {
  name: "CheckTemplate",
  components: {
    selectedIndicator
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 考核模版表格数据
      checkTemplateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 选择指标弹框
      indicatorOpen: false,
      // 已选指标
      notIndicatorIds: [],
      // 临时存放
      temporyList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        templateName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        templateName: [
          {required: true, message: "请输入模版名称", trigger: "blur"},
        ],
        checkRole: [
          {required: true, message: "请选择考核角色", trigger: "change"},
        ],
      }, // 职别列表
      categoryOptions: [],
    };
  },
  created() {
    this.getList();
    // 获取职别列表
    this.handleGetListCategory();
  },
  methods: {
    /** 查询考核模版列表 */
    getList() {
      this.loading = true;
      listCheckTemplate(this.queryParams).then((response) => {
        this.checkTemplateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 职别列表
    handleGetListCategory() {
      getListCategory().then(res => {
        this.categoryOptions = res.data;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        templateId: null,
        templateName: null,
        checkRole: null,
        sumWeight: null,
        indicatorList: [],
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加考核模版";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const templateId = row.templateId || this.ids;
      getCheckTemplate(templateId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改考核模版";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if(this.form.sumWeight > 100){
            this.$message.error('指标总权重不能大于100！');
            return;
          }
          if (this.form.templateId != null) {
            updateCheckTemplate(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCheckTemplate(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const templateIds = row.templateId || this.ids;
      this.$modal
        .confirm('是否确认删除考核模版编号为"' + templateIds + '"的数据项？')
        .then(function () {
          return delCheckTemplate(templateIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {
        });
    },
    // 选择指标
    handleAddIndicator() {
      this.indicatorOpen = true;
    },
    // 指标选择回调
    handleIndicatorSelectionChange(selection) {
      this.temporyList = selection;
    },
    submitSelectedIndicator() {
      this.form.indicatorList = this.temporyList;
      this.indicatorOpen = false;
    },
    // 计算总权重
    calculatWeighte() {
      let sumWeight = 0;
      this.form.indicatorList.forEach(element => {
        sumWeight += parseFloat(element.weight);
      });
      this.form.sumWeight = sumWeight;
    }
  },
};
</script>
<style scoped>
.el-select {
  width: 100%;
}

::v-deep .el-table__cell{
  padding:0px;
}

::v-deep .cell{
  padding:0px;
}
</style>
